package com.bush.mall.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;

import com.bush.mall.common.entity.Result;
import com.bush.mall.common.exception.SaaSException;
import com.bush.mall.order.mapper.OrderReturnApplyMapper;
import com.bush.mall.order.entity.OrderReturnApplyEntity;
import com.bush.mall.order.service.OrderReturnApplyService;

/**
 * 订单退货申请
 * @author RUN
 * @date 2022-11-29
 */
@Service(OrderReturnApplyService.BEAN_ID)
@Slf4j
public class OrderReturnApplyServiceImpl extends ServiceImpl<OrderReturnApplyMapper, OrderReturnApplyEntity> implements OrderReturnApplyService {

    /**
     * 查询订单退货申请列表
     * @author RUN
     * @date 2022-11-29
     */
    @Override
    public Result queryForPage(Integer current,Integer size) {
        try{
            Assert.notNull(current,"参数异常");
            Assert.notNull(size,"参数异常");
            Page<OrderReturnApplyEntity> page = new Page<>(current,size);
            QueryWrapper<OrderReturnApplyEntity> wrapper = new QueryWrapper<>();
            wrapper.orderByDesc("create_time");
            baseMapper.selectPage(page,wrapper);
            return Result.ok(page);
        }catch(Exception e){
            log.error("OrderReturnApplyService queryForPage ERROR => {}" , e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    /**
     * 创建订单退货申请表单对象
     * @author RUN
     * @date 2022-11-29
     */
    @Override
    public Result saveOrderReturnApply(OrderReturnApplyEntity orderReturnApply) {
        try{
            Assert.notNull(orderReturnApply,"参数异常");
            boolean flag = this.save(orderReturnApply);
            if (flag) return Result.ok("订单退货申请添加成功");
            throw new SaaSException("订单退货申请添加失败");
        }catch(Exception e){
            log.error("OrderReturnApplyService saveOrderReturnApply ERROR => {}" , e.getMessage());
            return Result.error(e.getMessage());
        }
    }


    /**
     * 修改订单退货申请表单对象
     * @author RUN
     * @date 2022-11-29
     */
    @Override
    public Result updateOrderReturnApply(OrderReturnApplyEntity orderReturnApply) {
        try{
            Assert.notNull(orderReturnApply,"参数异常");
            boolean flag = this.updateById(orderReturnApply);
            if (flag) return Result.ok("订单退货申请修改成功");
            throw new SaaSException("订单退货申请修改失败");
        }catch(Exception e){
            log.error("OrderReturnApplyService updateOrderReturnApply ERROR => {}" , e.getMessage());
            return Result.error(e.getMessage());
        }
    }


    /**
     * 通过id删除订单退货申请表单
     * @author RUN
     * @date 2022-11-29
     */
    @Override
    public Result deleteOrderReturnApply(Long id) {
        try{
            Assert.notNull(id,"参数异常");
            boolean flag = this.removeById(id);
            if (flag) return Result.ok("订单退货申请删除成功");
            throw new SaaSException("订单退货申请删除失败");
        }catch(Exception e){
            log.error("OrderReturnApplyService deleteOrderReturnApply ERROR => {}" , e.getMessage());
            return Result.error(e.getMessage());
        }
    }




}